package com.user.query.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.user.query.model.Role;
import com.user.query.model.RoleMenu;
import com.user.query.service.MenuService;
import com.user.query.service.RoleMenuService;
import com.user.query.service.RoleService;
import com.user.query.utils.HttpCode;
import com.user.query.vo.RoleVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 角色控制器RoleController
 * 
 * @author lijk
 * @date 2015-10-27 下午8:28:12
 */
@Controller
@RequestMapping("/role/")
public class RoleController extends BaseController {

	@Resource
	private RoleService roleService;
	@Resource
	private MenuService menuService;
	@Resource
	private RoleMenuService roleMenuService;
	
	/**
	 * 获取角色信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("list")
	public ModelAndView list() {
		ModelAndView view = new ModelAndView("system/roleManager");
		try {
			view.addObject("menus", menuService.queryAllEffective());
			view.addObject("results", roleService.grep(new Role()));
		} catch (Exception e) {
			log.info("获取角色列表异常：" + e);
		}
		return view;
	}

	/**
	 * 角色更新
	 * 
	 * @param role
	 * @return
	 */
	@RequestMapping("modify")
	@ResponseBody
	public Map<String, Object> modify(RoleVo role, String menuIds) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			Role qRole = roleService.get(role.getRoleId());
			role.setRoleName(qRole.getRoleName());
			role.setMenuId(role.getIds());
			roleService.modify(role);
			resultMap.put("code", HttpCode.HTTP_CODE_200);
			resultMap.put("msg", "保存成功");
		} catch (Exception e) {
			log.info("角色更新异常：" + e);
			resultMap.put("code", HttpCode.HTTP_CODE_500);
			resultMap.put("msg", "保存失败");
		}
		return resultMap;
	}

	/**
	 * 获取角色信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("info")
	@ResponseBody
	public Map<String, Object> info(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			RoleMenu roleMenu = new RoleMenu();
			roleMenu.setRole(id);
			resultMap.put("role", roleService.get(id));
			resultMap.put("roleMenus", roleMenuService.queryModel(roleMenu));
			resultMap.put("code", HttpCode.HTTP_CODE_200);
			resultMap.put("msg", "查询成功");
		} catch (Exception e) {
			log.info("获取角色信息异常：" + e);
			resultMap.put("code", HttpCode.HTTP_CODE_500);
			resultMap.put("msg", "查询失败");
		}
		return resultMap;
	}

}
